using System.Collections;
using UnityEngine;

public interface ISkillImpact
{
    SkillImpactType ImpactType { get; }
    void Execute(SkillDeployer deployer);
}

public class CostSPImpact : ISkillImpact
{
    public SkillImpactType ImpactType => SkillImpactType.CostSP;

    public void Execute(SkillDeployer deployer)
    {
        CharacterStatus status = deployer.Data.owner.GetComponent<CharacterStatus>();
        status.mana -= deployer.Data.item.costSP;
    }
}

public class DamageImpact : ISkillImpact
{
    public SkillImpactType ImpactType => SkillImpactType.Damage;

    public void Execute(SkillDeployer deployer)
    {
        deployer.StartCoroutine(RepeatDamage(deployer));
    }

    /// <summary>
    /// 持续伤害
    /// </summary>
    private IEnumerator RepeatDamage(SkillDeployer deployer)
    {
        SkillData data = deployer.Data;
        float attackTime = 0;
        WaitForSeconds tempSecond = new WaitForSeconds(data.item.attackInterval);
        do
        {
            deployer.CalculateTargets();
            OnceDamage(deployer);
            yield return tempSecond;
            attackTime += data.item.attackInterval;
        } while (attackTime < data.item.durationTime);
    }

    /// <summary>
    /// 造成一次攻击
    /// </summary>
    public void OnceDamage(SkillDeployer deployer)
    {
        CharacterStatus characterStatus = deployer.Data.owner.GetComponent<CharacterStatus>();
        float damage = deployer.Data.item.warriorAttackRatio * characterStatus.warriorAttack + 
            deployer.Data.item.hunterAttackRatio * characterStatus.hunterAttack +
            deployer.Data.item.assasinAttackRatio * characterStatus.assasinAttack;

        for (int i = 0; i < deployer.Data.attackTargets.Count; i++)
        {
            var status = deployer.Data.attackTargets[i].GetComponent<CharacterStatus>();
            status.Damage(damage);
        }
    }
}